<template>
	<view class="uni-container">
		<uni-forms ref="form" :value="formData" validate-trigger="submit" err-show-type="toast">
			<uni-forms-item name="image_url" label="图片url" required>
				<uni-file-picker file-mediatype="image" return-type="object" v-model="formData.image_url" :imageStyles="imageStyles" />
			</uni-forms-item>
			<uni-forms-item name="open_url" label="点击目标地址">
				<uni-easyinput placeholder="点击跳转目标地址。如果是web地址则使用内置web-view打开；如果是本地页面则跳转本地页面；如果是schema地址则打开本地的app" v-model="formData.open_url"
				 trim="both" />
			</uni-forms-item>
			<uni-forms-item name="title" label="标题">
				<uni-easyinput placeholder="标题。为避免背景图和前景文字靠色导致看不清，建议使用把标题作为图片的一部分来处理。如可以保证不靠色，则可以使用本字段，或者额外扩展title_color、title_bgcolor等字段"
				 v-model="formData.title" trim="both" />
			</uni-forms-item>
			<uni-forms-item name="sort" label="排序">
				<uni-easyinput placeholder="排序" type="number" v-model="formData.sort" />
			</uni-forms-item>
			<uni-forms-item name="category_id" label="分类id">
				<uni-easyinput placeholder="多个栏目的banner都存在一个表里时可用这个字段区分" v-model="formData.category_id" />
			</uni-forms-item>
			<uni-forms-item name="status" label="状态">
				<switch @change="binddata('status', $event.detail.value)" :checked="formData.status" />
			</uni-forms-item>
			<uni-forms-item name="description" label="备注">
				<uni-easyinput placeholder="维护者自用描述" v-model="formData.description" trim="both" />
			</uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" @click="submit">提交</button>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '@/js_sdk/validator/opendb-banner.js';

	const db = uniCloud.database();
	const dbCollectionName = 'opendb-banner';

	function getValidator(fields) {
		let reuslt = {}
		for (let key in validator) {
			if (fields.indexOf(key) > -1) {
				reuslt[key] = validator[key]
			}
		}
		return reuslt
	}

	export default {
		data() {
			return {
				formData: {
					"image_url": null,
					"open_url": "",
					"title": "",
					"sort": null,
					"category_id": "",
					"status": true,
					"description": ""
				},
				formOptions: {},
				rules: {
					...getValidator(["image_url", "open_url", "title", "sort", "category_id", "status", "description"])
				},
				imageStyles: {
					width: 64,
					height: 64
				}
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			/**
			 * 触发表单提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.submit().then((res) => {
					console.log(res);
					this.submitForm(res)
				}).catch((errors) => {
					uni.hideLoading()
				})
			},

			submitForm(value) {
				// 使用 clientDB 提交数据
				db.collection(dbCollectionName).add(value).then((res) => {
					uni.showToast({
						icon: 'none',
						title: '新增成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			}
		}
	}
</script>

<style>
	.uni-container {
		padding: 15px;
	}

	.uni-input-border,
	.uni-textarea-border {
		width: 100%;
		font-size: 14px;
		color: #666;
		border: 1px #e5e5e5 solid;
		border-radius: 5px;
		box-sizing: border-box;
	}

	.uni-input-border {
		padding: 0 10px;
		height: 35px;

	}

	.uni-textarea-border {
		padding: 10px;
		height: 80px;
	}

	.uni-button-group {
		margin-top: 50px;
		display: flex;
		justify-content: center;
	}

	.uni-button {
		width: 184px;
		padding: 12px 20px;
		font-size: 14px;
		border-radius: 4px;
		line-height: 1;
		margin: 0;
	}
</style>
